﻿/*************************************************************************************
 * 文 件 名:   HK_DropdownEditor
 * 
 * 描    述:   重写的UGUI Dropdown组件，通过在Inspector面板中修改AudioType播放对应的Button音效
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭、郭加和
 * 创建时间：  2022/12/7
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UnityEditor.UI
{
    [CustomEditor(typeof(HK_Dropdown), true)]
    [CanEditMultipleObjects]
    public class HK_DropdownEditor : DropdownEditor
    {
        SerializedProperty audioPressType;
        SerializedProperty audioSelectType;

        protected override void OnEnable()
        {
            base.OnEnable();
            audioPressType = serializedObject.FindProperty("audioPressType");
            audioSelectType = serializedObject.FindProperty("audioSelectType");
        }
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            serializedObject.Update();
            EditorGUILayout.Space(5);
            EditorGUILayout.PropertyField(audioPressType);
            serializedObject.Update();
            EditorGUILayout.Space(10);
            EditorGUILayout.PropertyField(audioSelectType);
            serializedObject.ApplyModifiedProperties();
        }
    }
    //创建HK_Dropdown
    public class HK_CreateDropdown
    {
        static Transform parent_TF;

        [MenuItem("GameObject/UI/HK_Dropdown", false, 30)]
        static void CreateDropdown()
        {
            if (Selection.activeGameObject != null)
            {
                parent_TF = Selection.activeGameObject.transform;
            }

#if UNITY_2021_2_OR_NEWER
            EditorApplication.ExecuteMenuItem("GameObject/UI/Legacy/Dropdown");
#else   
            EditorApplication.ExecuteMenuItem("GameObject/UI/Dropdown");
#endif
            //获取生成的hk_Dropr物体
            GameObject hk_Drop = Selection.activeGameObject;

            if (parent_TF != null)
            {
                Undo.SetTransformParent(hk_Drop.transform, parent_TF, "undo");
                parent_TF = null;
            }

            hk_Drop.name = "HK_Dropdown";
            //销毁原有的Dropdown组件
            GameObject.DestroyImmediate(hk_Drop.GetComponent<Dropdown>(), false);

            hk_Drop.AddComponent<HK_Dropdown>();
            hk_Drop.transform.GetComponentInChildren<Text>().text = "HK_Dropdown";
            hk_Drop.transform.GetComponent<HK_Dropdown>().template = hk_Drop.transform.Find("Template").GetComponent<RectTransform>();
            hk_Drop.transform.GetComponent<HK_Dropdown>().captionText = hk_Drop.transform.GetChild(0).GetComponent<Text>();
            hk_Drop.transform.GetComponent<HK_Dropdown>().itemText = hk_Drop.transform.Find("Template/Viewport/Content/Item/Item Label").GetComponent<Text>();

            //添加options
            List<HK_Dropdown.OptionData> listOptions = new List<HK_Dropdown.OptionData>();
            listOptions.Add(new HK_Dropdown.OptionData("HK_DropdownA"));
            listOptions.Add(new HK_Dropdown.OptionData("HK_DropdownB"));
            listOptions.Add(new HK_Dropdown.OptionData("HK_DropdownC"));
            hk_Drop.transform.GetComponent<HK_Dropdown>().AddOptions(listOptions);
        }
    }
}


